﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Convertor.Interface;
using Convertor.Plugin;

namespace Convertor
{
    /// <summary>
    /// LoadConfigForm.xaml 的交互逻辑
    /// </summary>
    public partial class LoadConfigForm : Window, IFormGetConfigEvent
    {
        private static readonly string configPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.AdminTools), @"Convertor\History.data"); //配置文件保存位置

        ISelectedPluginContent pluginContent;
        object plugin;
        UserControl configForm;
        List<HistoryContent> historyList;

        public LoadConfigForm()
        {
            InitializeComponent();
        }

        public LoadConfigForm(ISelectedPluginContent pluginContent, object plugin)
            : this()
        {
            this.pluginContent = pluginContent;
            this.plugin = plugin;
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            IPluginConfigForm iFrom = (IPluginConfigForm)plugin;
            configForm = iFrom.ConfigForm;
            ((IPluginConfig)configForm).Config = pluginContent.Config;
            PluginLoader.SetValue(ContentPresenter.ContentProperty, null);
            configForm.Margin = new Thickness(5);
            PluginLoader.Children.Add(configForm);
            IPluginHistory iPluginHistory = configForm as IPluginHistory;
            if (iPluginHistory != null)
            {
                try
                {
                    historyList = Wpf.CommonTools.FileExtension.Processing.LoadFile<List<HistoryContent>>(configPath);
                }
                catch (Exception ex)
                {
                    historyList = new List<HistoryContent>();
                }
                iPluginHistory.InputHistory = historyList.Where(p => p.Path == pluginContent.Path).Select(p => p.Hisotry).ToList();
            }

            this.SizeToContent = SizeToContent.Manual;
            this.SizeToContent = SizeToContent.Width;
            this.SizeToContent = SizeToContent.Height;
            this.SizeToContent = SizeToContent.WidthAndHeight;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            setConfig();
            saveHistory();

            this.DialogResult = true;
            this.Close();
        }

        private void setConfig()
        {
            IPluginConfigForm iFrom = (IPluginConfigForm)plugin;
            pluginContent.Config = ((IFormGetConfigEvent)configForm).GetConfig();
        }

        private void saveHistory()
        {
            IPluginHistory iPluginHistory = configForm as IPluginHistory;
            if (iPluginHistory != null)
            {
                var input = iPluginHistory.Input;
                HistoryContent content = new HistoryContent() { Path = pluginContent.Path, Hisotry = input };
                if (!historyList.Contains(content))
                {
                    var history = historyList.Where(p => p.Path == pluginContent.Path).ToList();
                    if (history.Count > 15)
                    {
                        historyList.Remove(history[history.Count - 1]);
                    }
                    historyList.Insert(0, content);
                }
                else
                {
                    historyList.Remove(content);
                    historyList.Insert(0, content);
                }
                try
                {
                    Wpf.CommonTools.FileExtension.Processing.SaveFile(historyList, configPath);
                }
                catch (Exception ex)
                {
                }
            }
        }

        #region IConfigFormEvent 成员

        object IFormGetConfigEvent.GetConfig()
        {
            return pluginContent.Config;
        }

        #endregion

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
